<template>
  <div class="container">
    <div class="title">
      <div class="text1">订单详情</div>
      <div class="text2"><img :src="icon" alt="" />{{ title }}</div>
      <div class="text3">订单编号：{{ data.out_trade_no }}</div>
      <div class="outTime" v-if="data.status == 0">
        剩余{{ outTime }}后订单自动关闭
      </div>
    </div>
    <div class="address">
      <img src="../../assets/icon11.png" alt="" />
      <div class="content">
        <div class="aqian">
          {{ data.bank.username }}
          <span style="font-size: 0.8em; color: #333333">{{
            data.bank.mobile
          }}</span>
        </div>
        <div class="number">
          银行卡号:
          <span style="font-size: 0.8em; color: #333333">{{
            data.bank.banknum
          }} ({{ data.bank.username }})</span>
        </div>
      </div>
    </div>
    <div class="substance" @click="navGoods">
      <img :src="data.goods.list_img" alt="" />
      <div class="content">
        <div class="name">{{ data.goods.name }}</div>
        <div class="comNumber">
          商品编号：<span style="color: #333333">{{
            data.goods.goods_num
          }}</span>
        </div>
        <div class="conName">
          商 家：<span style="color: #333333">{{ data.goods.username }}</span>
        </div>
        <div class="time">
          抢购时间：<span style="color: #333333">{{ data.create_date }}</span>
        </div>
      </div>
    </div>
    <div class="mouny">
      <div>商品总价</div>
      <div>
        ￥ <span style="font-size: 1.6em">{{ data.price }}</span>
      </div>
    </div>
    <div class="button">
      <van-button
        @click="submit"
        type="danger"
        class="vanButton"
        round
        v-if="type == 0"
        >立即支付</van-button
      >
      <van-button
        @click="submit"
        type="danger"
        class="vanButton"
        color="#E5E5E5"
        round
        v-else-if="type == 1"
        >查看凭证</van-button
      >
      <!-- <van-button
        @click="submit"
        type="danger"
        class="vanButton"
        color="#3ACE8A"
        round
        v-else-if="type == 2"
        >上架商品</van-button
      > -->
    </div>

    <van-dialog
      v-model="show"
      :title="`请上传支付凭证，金额${data.price}元`"
      @confirm="onConfirm"
      showCancelButton
      confirmButtonText="上传凭证"
    >
      <img :src="pingzhengImg" class="dialogImg" />
    </van-dialog>

    <van-dialog v-model="pzshow" title="支付凭证">
      <img :src="data.pingzheng" class="dialogImg" />
    </van-dialog>

    <van-dialog v-model="payShowTwo" :showConfirmButton="false">
      <img :src="imageUrl" class="dialogImg" width="50" height="300" />
      <div class="dialogTitle">
        <van-button type="default" class="myButton" @click="tijiao(0)"
          >重选</van-button
        >
        <van-button type="primary" class="myButton" @click="tijiao(1)"
          >确定</van-button
        >
      </div>
    </van-dialog>

    <van-dialog
      v-model="pamShow"
      class="diaImg"
      closeOnClickOverlay
      :showConfirmButton="false"
    >
      <div class="image">
        <img src="../../assets/img5.png" alt="" />
      </div>
      <div class="butList">
        <van-uploader
          class="uploader"
          result-type="file"
          :after-read="afterRead"
        >
          <van-button type="primary" class="diaButton" accept="image/*"
            >相册选择</van-button
          >
        </van-uploader>
        <van-uploader
          class="uploader"
          :after-read="afterRead"
          result-type="file"
          capture="camera"
          accept="image/*"
        >
          <van-button plain type="primary" class="diaButton"
            >拍摄凭证</van-button
          >
        </van-uploader>
      </div>
    </van-dialog>
  </div>
</template>

<script>
import {
  getOrder,
  upPingzheng,
  upLoad,
  imgUrl,
  getCardInfo,
  getConfig,
} from "../../api/api";
import icon8 from "../../assets/icon8.png";
import icon9 from "../../assets/icon9.png";
import icon10 from "../../assets/icon10.png";
export default {
  data() {
    return {
      title: "",
      type: 0,
      icon: "",
      data: {},
      show: false,
      pamShow: false,
      fileList: [],
      payShowTwo: false,
      imageUrl: "",
      pingzheng_url: "",
      pingzhengImg: "",
      pzshow: false,
      outTime: "",
      setTime: null,
    };
  },
  methods: {
    getTitle() {
      let title;
      let icon;
      switch (this.type) {
        case 0:
          title = "等待买家付款";
          icon = icon8;
          break;

        case 1:
          title = "管理员审核中，请耐心等待…";
          icon = icon9;
          break;

        case 2:
          title = "付款成功";
          icon = icon10;
          break;

        default:
          break;
      }
      this.title = title;
      this.icon = icon;
    },
    submit() {
      switch (this.type) {
        case 0:
          this.navPay();
          break;

        case 1:
          this.getInfo();
          break;

        case 2:
          this.navCom();
          break;

        default:
          break;
      }
    },
    navPay() {
      console.log("支付");
      // if (this.data.goods.user_id == 0) {
      //   this.pingzhengImg =
      //     imgUrl + JSON.parse(localStorage.getItem("config")).skm_qrcode;
      // } else {
      //   this.pingzhengImg = imgUrl + this.data.qrcode_url;
      // }
      this.pingzhengImg =
        imgUrl + JSON.parse(localStorage.getItem("config")).skm_qrcode;
      this.show = true;
      // this.pamShow = true;
    },
    onConfirm() {
      this.pamShow = true;
    },
    getInfo() {
      console.log("查看凭证");
      this.data.pingzheng = imgUrl + this.data.pingzheng;
      this.pzshow = true;
    },
    navCom() {
      console.log("上架商品");
      this.$router.push({
        path: "/goods",
        query: {
          goods_id: this.data.goods_id,
        },
      });
    },
    afterRead(file) {
      this.fileList.push(file.file);
      this.pamShow = false;
      this.payShowTwo = true;
      this.imageUrl = file.content;
      console.log(file.file);
      let param = new FormData();
      param.append("file", file.file);
      console.log("上传参数---", param);
      upLoad(param).then((res) => {
        this.$toast.clear();
        console.log("上传图片接口---", res);
        this.imageUrl = imgUrl + res.data.url;
        this.pingzheng_url = res.data.url;
      });
    },
    navGoods() {
      this.$router.push({
        path: "/details",
        query: {
          item: JSON.stringify(this.data.goods),
        },
      });
    },
    tijiao(type) {
      if (type == 0) {
        this.payShowTwo = false;
        this.pamShow = true;
      } else {
        const data = {
          user_id: localStorage.getItem("userId"),
          order_id: this.$route.query.item.order_id,
          pingzheng_url: this.pingzheng_url,
        };
        upPingzheng(data).then((res) => {
          this.$toast.clear();
          console.log("上传凭证接口---", res);
          this.$toast.success(res.msg);
          this.payShowTwo = false;
          this.getOrderInfo();
        });
      }
    },
    getOrderInfo() {
      const data = {
        user_id: localStorage.getItem("userId"),
        order_num: this.$route.query.item.order_num,
      };
      getOrder(data).then((res) => {
        this.$toast.clear();
        this.data = res.data;
        this.type = res.data.status;
        res.data.goods.list_img = imgUrl + res.data.goods.list_img;
        if (res.data.pingzheng != "") this.pingzhengImg = res.data.pingzheng;

        const date = new Date(res.data.create_date * 1000);
        const fullYear = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        const hours = date.getHours();
        const minutes = date.getMinutes();

        if (res.data.status == 0) {
          const oldTime = new Date(
            res.data.create_date * 1000 +
              JSON.parse(localStorage.getItem("config")).zdtktime * 60 * 1000
          );
          const newTime = new Date();
          const oldDate = parseInt((oldTime.getTime() - newTime.getTime())/1000);
          const newMinutes = parseInt(oldDate / 60);
          const newSeconds = parseInt(oldDate % 60);
          if (oldDate > 0) {
            this.outTime = `${
              newMinutes >= 10 ? newMinutes : `0${newMinutes}`
            } : ${newSeconds >= 10 ? newSeconds : `0${newSeconds}`}`;
          } else {
            this.outTime = "00 : 00";
          }

          if(this.setTime != '') return

          this.setTime = setInterval(() => {
            const newTime = new Date();
            const oldDate = parseInt((oldTime.getTime() - newTime.getTime())/1000);
            const newMinutes = parseInt(oldDate / 60);
            const newSeconds = parseInt(oldDate % 60);
            console.log('----------------', oldDate)

            if (oldDate < 0) {
              this.outTime = "00 : 00";
              clearInterval(this.setTime);
              this.$dialog
                .alert({
                  message: `下单时间为 ${hours} : ${minutes>10?minutes:'0'+minutes}, ${JSON.parse(localStorage.getItem("config")).zdtktime}分钟内订单未付款，已自动关闭`,
                  theme: "round-button",
                })
                .then(() => {
                  this.$router.go(-1);
                  this.outTime = null;
                });
            } else {
              this.outTime = `${
                newMinutes >= 10 ? newMinutes : `0${newMinutes}`
              } : ${newSeconds >= 10 ? newSeconds : `0${newSeconds}`}`;
            }
            // console.log(this.outTime);
          }, 1000);
        }
        
        res.data.create_date = `${fullYear}-${month}-${day} ${hours}:${minutes>10?minutes:'0'+minutes}`;
        this.getTitle();
        console.log("获取订单详情接口---", this.data);
      });
    },
    async getConfigInfo() {
      const { data } = await getConfig();
      localStorage.setItem("config", JSON.stringify(data[0]));
      // getConfig().then((res) => {
      //   this.$toast.clear();
      //   // localStorage.setItem("config", JSON.stringify(res.data[0]));
      //   console.log("公共-获取配置信息接口", res.data[0]);
      // });
    },
  },
  mounted() {
    this.$route.query.item = JSON.parse(this.$route.query.item);
    if (!this.$route.query.item)
      return this.$dialog
        .alert({
          message: `订单已被抢购`,
          theme: "round-button",
        })
        .then(() => {
          this.$router.go(-1);
        });
    console.log("this.$route.query.item---", this.$route.query.item);
    this.getOrderInfo();
    if (this.data.status == 0)
      this.$dialog
        .alert({
          message: `请在${
            JSON.parse(localStorage.getItem("config")).zdtktime
          }分钟内支付，超时订单自动取消`,
          theme: "round-button",
        })
        .then(() => {});
  },
  created() {
    this.getConfigInfo();
  },
  beforeDestroy() {
    clearInterval(this.setTime);
  },
};
</script>

<style lang="less" scoped>
div {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.container {
  min-height: 100vh;
  background-color: #f1f1f1;

  .title {
    width: 100%;
    height: 25vh;
    background-color: #3ace8a;
    color: #fff;
    letter-spacing: 1px;

    .text1 {
      width: 100%;
      height: 60px;
      text-align: center;
      line-height: 60px;
      font-size: 1.2em;
    }

    .text2 {
      margin-top: 1rem;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      font-size: 1.2em;

      img {
        width: 20px;
        margin-left: 10px;
        margin-right: 10px;
      }
    }
    .text3 {
      margin-top: 0.8rem;
      margin-left: 10px;
      font-size: 0.8em;
      display: flex;
      justify-content: flex-start;
      align-items: center;
    }
    .outTime {
      font-size: 0.9em;
      color: #df0000;
      margin-top: 0.8rem;
      margin-left: 10px;
      font-weight: 700;
    }
  }
  .address {
    width: 100%;
    height: 100px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    background-color: #fff;

    img {
      width: 40px;
      height: 27px;
      margin-left: 40px;
    }

    .content {
      margin-left: 20px;

      div {
        font-size: 1em;
      }

      .number {
        margin-top: 10px;
        color: #333333;
        font-size: 0.8em;
      }
    }
  }
  .substance {
    width: 100%;
    height: 150px;
    margin-top: 10px;
    padding: 15px;
    background-color: #fff;
    display: flex;
    justify-content: center;
    align-items: flex-start;

    img {
      width: 100px;
    }
    .content {
      width: calc(100% - 85px);
      height: 95%;
      margin-left: 15px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-items: flex-start;

      .name {
        font-size: 1em;
        color: #333333;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        overflow: hidden;
        text-overflow: ellipsis;
        -webkit-box-orient: vertical;
      }

      .comNumber {
        margin-top: 10px;
        font-size: 0.8em;
        color: #999999;
      }
      .conName {
        // margin-top: 5px;
        font-size: 0.8em;
        color: #999999;
      }
      .time {
        font-size: 0.8em;
        color: #999999;
      }
    }
  }
  .mouny {
    width: 100%;
    height: 80px;
    margin-top: 10px;
    padding: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #fff;

    div:nth-child(1) {
      color: #333333;
      font-size: 0.8em;
    }
    div:nth-child(2) {
      color: #df0000;
      font-weight: 700;
      font-size: 0.8em;
    }
  }
  .button {
    width: 100%;
    height: 50px;
    position: fixed;
    bottom: 0;
    padding: 15px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    background-color: #fff;

    .vanButton {
      width: 120px;
      height: 40px;
      letter-spacing: 1px;
    }
  }
  .dialogImg {
    width: 90%;
    margin-left: 5%;
    margin-top: 20px;
  }

  .dialogTitle {
    width: 100%;
    height: 50px;
    font-size: 1em;
    text-align: center;
    line-height: 40px;

    .myButton {
      width: 100px;
      height: 30px;
      border-radius: 5px;
      margin-right: 10px;
      margin-left: 10px;
    }
  }

  .diaImg {
    box-sizing: border-box;
    .image {
      width: 70%;
      margin: 40px auto 0px;
      //   margin-left: 17.5%;
      height: 150px;
      text-align: center;
      border: 1px solid #e5e5e5;

      img {
        height: 70%;
        margin-top: 10%;
      }
    }

    .butList {
      width: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      padding-bottom: 20px;

      .diaButton {
        width: 220px;
        border-radius: 5px;
        margin-top: 10px;
      }
    }
  }
}
</style>